package com.example.recruitment_website.controller;
//import org.junit.jupiter.api.Test;

import com.example.recruitment_website.domain.Test;
import com.example.recruitment_website.service.TestService;
import com.example.recruitment_website.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

@Slf4j
@RequestMapping("/redis")
@RestController
public class TestController {

    @Autowired
    private TestService testService;

    @Autowired
    private JdbcTemplate jdbcTemple;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    private static int ExpireTime = 60;

    @Resource
    private RedisUtil redisUtil;


    @RequestMapping("set")
    public boolean redisset(String key, String value){
        Test test =new Test();
        test.setId(10001);
        test.setTitle("123");
        test.setContain("321");

        return redisUtil.set(key,value);
    }
    @RequestMapping("get")
    public Object redisget(String key){
        return redisUtil.get(key);
    }

    @RequestMapping("expire")
    public boolean expire(String key){
        return redisUtil.expire(key,ExpireTime);
    }


    @RequestMapping("/index")
    public List<Map<String,Object>> index(){
        String sql = "SELECT * FROM employee";
        List<Map<String,Object>> list = jdbcTemple.queryForList(sql);
        for(Map<String,Object> map:list){
            Set<Map.Entry<String,Object>> entries = map.entrySet();
            if(entries!=null){
                Iterator<Map.Entry<String,Object>> iterator = entries.iterator();
                while (iterator.hasNext()){
                    Map.Entry<String,Object> entry = iterator.next();
                    Object key = entry.getKey();
                    Object value = entry.getValue();
//                    System.out.println(key+":"+value);
                }
            }
        }
        return list;
    }


    @RequestMapping("/findAllTest")
    public HashMap findAllTest(){
        List<Test> testList = testService.findAllTest();
        HashMap<String,Object> map = new HashMap<>();
        map.put("data",testList);
        map.put("msg","请求成功");
        map.put("code",0);
        return map;
    }
}